package com.example.grape_effect.effet.filter

import com.example.grape_effect.gl.common.ShaderParameter


class SharpenFilter: LYFilter() {
    init {
        name = "sharpenFilter"
    }
    override var vertexShader ="""
        attribute vec4 vPosition;
        attribute vec4 inputTextureCoordinate;

        uniform float imageWidthFactor;
        uniform float imageHeightFactor;
        uniform float sharpness;

        varying vec2 textureCoordinate;
        varying vec2 leftTextureCoordinate;
        varying vec2 rightTextureCoordinate;
        varying vec2 topTextureCoordinate;
        varying vec2 bottomTextureCoordinate;

        varying float centerMultiplier;
        varying float edgeMultiplier;

        void main()
        {
            gl_Position = vPosition;

            mediump vec2 widthStep = vec2(imageWidthFactor, 0.0);
            mediump vec2 heightStep = vec2(0.0, imageHeightFactor);

            textureCoordinate = inputTextureCoordinate.xy;
            leftTextureCoordinate = inputTextureCoordinate.xy - widthStep;
            rightTextureCoordinate = inputTextureCoordinate.xy + widthStep;
            topTextureCoordinate = inputTextureCoordinate.xy + heightStep;
            bottomTextureCoordinate = inputTextureCoordinate.xy - heightStep;

            centerMultiplier = 1.0 + 4.0 * sharpness;
            edgeMultiplier = sharpness;
        }
    """.trimIndent()
    override var fragmentShader = """
         precision highp float;
 varying highp vec2 textureCoordinate;
 varying highp vec2 leftTextureCoordinate;
 varying highp vec2 rightTextureCoordinate;
 varying highp vec2 topTextureCoordinate;
 varying highp vec2 bottomTextureCoordinate;

 varying highp float centerMultiplier;
 varying highp float edgeMultiplier;

 uniform sampler2D inputTexture;

 void main()
 {
     mediump vec3 textureColor = texture2D(inputTexture, textureCoordinate).rgb;
     mediump vec3 leftTextureColor = texture2D(inputTexture, leftTextureCoordinate).rgb;
     mediump vec3 rightTextureColor = texture2D(inputTexture, rightTextureCoordinate).rgb;
     mediump vec3 topTextureColor = texture2D(inputTexture, topTextureCoordinate).rgb;
     mediump vec3 bottomTextureColor = texture2D(inputTexture, bottomTextureCoordinate).rgb;

     gl_FragColor = vec4((textureColor * centerMultiplier - (leftTextureColor * edgeMultiplier + rightTextureColor * edgeMultiplier + topTextureColor * edgeMultiplier + bottomTextureColor * edgeMultiplier)), texture2D(inputTexture, bottomTextureCoordinate).w);
 }

    """.trimIndent()

    override fun addParameters() {
        super.addParameters()
        parameters["sharpness"] = ShaderParameter(
            "sharpness",
            ShaderParameter.TYPE_UNIFORM,
            ShaderParameter.VALUE_TYPE_FLOAT
        )
        parameters["imageWidthFactor"] = ShaderParameter(
            "imageWidthFactor",
            ShaderParameter.TYPE_UNIFORM,
            ShaderParameter.VALUE_TYPE_FLOAT
        )
        parameters["imageHeightFactor"] = ShaderParameter(
            "imageHeightFactor",
            ShaderParameter.TYPE_UNIFORM,
            ShaderParameter.VALUE_TYPE_FLOAT
        )
    }
    /**
     * @sharpness: from -4.0 to 4.0, with 0.0 as the normal level
     */
    override fun setDefaultParameterValues() {
        super.setDefaultParameterValues()
        parameters["sharpness"]?.value = 1f
        parameters["imageWidthFactor"]?.value = 2/1080f
        parameters["imageHeightFactor"]?.value = 2/1440f
    }

}